# !usr/bin/env python
# -*- coding:utf-8 _*-
"""
@Author:张广勤
@Web site: https://www.tunan.wang
@Github:www.github.com
 
@File:xinzen1_0.py.py
@Time:2024/12/4 15:16

@Motto:不积跬步无以至千里，不积小流无以成江海！
"""
"""
获得前一天（昨天）的日期
获得之前新闻截止日期
获取从截止日期次日，到昨天的新闻链接
追加到之前新闻链接后
"""
from cctv_news import date_list1_0,news1_2
from cctv_news import newdate1_0,datetime1_0
from tqdm import tqdm
import csv
# 给定的起始和最后日期
# start_date = '20240101'
# csv_file_path = '../news/outputnews_2024.csv'
csv_file_path = 'news/outputnews_2024.csv'
start_date = newdate1_0.find_latest_date(csv_file_path)
# end_date = '20240310'
end_date = datetime1_0.date_yesterday()

# 构造日期列表
date_list = date_list1_0.generate_date_list(start_date, end_date)

def xinzen():
    # 打印日期列表
    data = []
    if date_list:
        for date in tqdm(date_list):
            datetime, titles, links = news1_2.news_xinzen(date.strftime('%Y%m%d'))
            for i in range(len(titles)):
                # print(datetime, titles[i], links[i])
                data.append({
                    "日期": datetime,
                    "标题": titles[i],
                    "链接": links[i]
                })
        # print(date.strftime('%Y%m%d'))
        # print(data)
        # 打开CSV文件并追加新行
        with open(csv_file_path, mode='a', newline='', encoding='utf-8') as file:
            writer = csv.DictWriter(file, fieldnames=['日期', '标题', '链接'])
            # 写入新行
            for row in data:
                writer.writerow(row)
        print("新行已追加到CSV文件。")
    else:
        pass
        # print("没有最新数据")


if __name__ == "__main__":
    xinzen()